package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.ProduceOrderDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.zhanghai.android.materialratingbar.MaterialRatingBar;

/**
 * Created by 朱俊鑫 on 2018
 */

public class MerchandiseEvalutatetActivity extends AppCompatActivity {
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.submit_bt)
    RelativeLayout submitBt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.produce)
    ImageView produce;
    @BindView(R.id.produce_name)
    TextView produceName;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.jifen_money)
    TextView jifenMoney;
    @BindView(R.id.currentprice_tv)
    TextView currentpriceTv;
    @BindView(R.id.originprice_tv)
    TextView originpriceTv;
    @BindView(R.id.redirectstore_bt)
    RelativeLayout redirectstoreBt;
    @BindView(R.id.guest_tv)
    TextView guestTv;
    @BindView(R.id.startbar)
    MaterialRatingBar startbar;
    @BindView(R.id.merchandise_tv)
    TextView merchandiseTv;
    @BindView(R.id.merchandise_starbar)
    MaterialRatingBar merchandiseStarbar;
    @BindView(R.id.merchandise_service_tv)
    TextView merchandiseServiceTv;
    @BindView(R.id.merchandise_servicestar)
    MaterialRatingBar merchandiseServicestar;
    @BindView(R.id.edit_text)
    EditText editText;
    private Dialog loadingdialog;
    private ProduceOrderDomain.DataBean dataBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchandiseevalutate);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        dataBean= (ProduceOrderDomain.DataBean) getIntent().getSerializableExtra("data");
        initComponent();
    }

    private void initComponent() {
        this.loadingdialog = new Dialog(this);
        this.loadingdialog.setContentView(R.layout.dialog_loading);
        this.loadingdialog.setCanceledOnTouchOutside(false);
        this.loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if(dataBean!=null){
            ProduceOrderDomain.DataBean.ProductOrderProductInfoBeansBean    data=dataBean.getProductOrderProductInfoBeans().get(0);
            Glide.with(this).load("" +data.getProductImg())
                    .placeholder(getResources().getColor(R.color.default_lightgray)).animate(R.anim.fade_in).override(300, 300).into(produce);
            this.produceName.setText(data.getProductName()+ "");
            this.num.setText("数量：" +data.getCount() + "");
            this.currentpriceTv.setText("现价：¥" +dataBean.getPrice());
            this.originpriceTv.setText("原价：¥" + dataBean.getOriginalPrice());
            this.originpriceTv.getPaint().setFlags(8);
            this.originpriceTv.getPaint().setFlags(16);
            this.originpriceTv.getPaint().setAntiAlias(true);
            this.originpriceTv.getPaint().setFlags(17);
        }
    }

    @OnClick(value = {R.id.backbt,R.id.submit_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.submit_bt:
                if ((this.startbar.getRating() == 0.0F) || (this.startbar.getRating() == 0.0D))
                {
                    Toast.makeText(getApplicationContext(), "请评价物流星级", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ((this.merchandiseStarbar.getRating() == 0.0F) || (this.merchandiseStarbar.getRating() == 0.0D))
                {
                    Toast.makeText(getApplicationContext(), "请评价商品星级",  Toast.LENGTH_SHORT).show();
                    return;
                }
                if ((this.merchandiseServicestar.getRating() == 0.0F) || (this.merchandiseServicestar.getRating() == 0.0D))
                {
                    Toast.makeText(getApplicationContext(), "请评价服务星级",  Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.editText.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "请说说你的感受",  Toast.LENGTH_SHORT).show();
                    return;
                }
                ProduceOrderDomain.DataBean.ProductOrderProductInfoBeansBean    data=dataBean.getProductOrderProductInfoBeans().get(0);
                insert(""+data.getProductId(),startbar.getRating()+"",merchandiseStarbar.getRating()+"",
                        merchandiseServicestar.getRating()+"",editText.getText().toString().trim());
                break;
        }
    }

  public  void insert(String merchandiseId,String logisticsStars,String storeStars,String serviceStarts,String content){
             List<User> userList= Xutils.queryAll(getApplication(),User.class);
             if(userList==null||userList.size()==0){
                 return;
             }
System.out.println("****************************"+dataBean.getOrderId());
      this.loadingdialog.show();
      Map<String,String >  map=new HashMap<>();
      map.put("orderId",""+dataBean.getOrderId());
      map.put("merchandiseId", "" + merchandiseId);
      map.put("logisticsStars", "" + logisticsStars);
      map.put("storeStars", "" + storeStars);
      map.put("serviceStarts", "" + serviceStarts);
      map.put("content", "" + content);
      map.put("userId", "" + userList.get(0).getUserId());
     Xutils.POST(HttpUrl.merevaluatemerchandiseadd, map, new Callback.CommonCallback<String>() {
         @Override
         public void onSuccess(String result) {
             MerchandiseEvalutatetActivity.this.loadingdialog.dismiss();
             try {
                 JSONObject data=new JSONObject(result);
                 Toast.makeText(MerchandiseEvalutatetActivity.this.getApplicationContext(), data.getString("message"), Toast.LENGTH_SHORT).show();
           switch (data.getInt("state")){
               case HttpCode.SUCCESS:
                   setResult(RESULT_OK);
                   finish();
                   break;
           }
             } catch (JSONException e) {
                 e.printStackTrace();
             }
         }

         @Override
         public void onError(Throwable ex, boolean isOnCallback) {
             MerchandiseEvalutatetActivity.this.loadingdialog.dismiss();
             Toast.makeText(MerchandiseEvalutatetActivity.this.getApplicationContext(),"请稍后再试", Toast.LENGTH_SHORT).show();
         }

         @Override
         public void onCancelled(CancelledException cex) {

         }

         @Override
         public void onFinished() {

         }
     });
  }


}
